<?php

namespace backend\modules\business\controllers;

use backend\controllers\BaseController;
use yii\helpers\Url;
use yii\data\Pagination;
use common\models\BusEmployee;
use common\models\Status;
use common\models\BusEmployeeReward;
/**
 * 员工管理
 * @author      futao
 * @date        2016-2-22
 * @version     2.0
 * @copyright   Copyright 2016 meelier.com
 */
class EmployeeController extends BaseController
{
	public function actionIndex()
	{
		$this->redirect(Url::toRoute('employee/list'));
	}
	/**
	 * 员工列表
	 * @date: 2016-2-24 
	 * @author: futao
	 */
	public function actionList()
	{
		$filterDate = $this->request->get('filter_date','');//时间段的业绩
		$where = $this->where;
		$where[] = 'del_flag = :state';
		$bind[':state'] = Status::NORMAL;
		$whereStr = implode(' and ',$where);
		$query = BusEmployee::find()
		->where($whereStr,$bind);
		$queryCount = clone $query;
		$page = new Pagination(['totalCount' => $queryCount->count(),'pageSize' => $this->pageNavLimit]);
		$list = $query
		->offset($page->offset)
		->limit($page->limit)
		->asArray()
		->all();
		if($filterDate!=''){
			$filterDateArr = explode('至',$filterDate);
			$startTime = trim($filterDateArr[0]);
			$endTime = trim($filterDateArr[1]);
		}
		foreach ($list as $k=>$v){
			unset($rewardWhere);
			unset($rewardBind);
			if($filterDate!=''){
				$rewardWhere[] = 'create_time >= :starttime';
				$rewardBind[':starttime'] = $startTime.' 00:00:00';
				$rewardWhere[] = 'create_time <= :endtime';
				$rewardBind[':endtime'] = $endTime.' 59:59:59';
			}
			$rewardWhere[] = 'employee_id = :id';
			$rewardBind[':id'] = $list[$k]['employee_id'];
			$rewardWhereStr = implode(' and ',$rewardWhere);
			$employeeReward[] = BusEmployeeReward::find()
			->select([
				'sum(sell_reward) as sell_count',//销售业绩
				'sum(consume_reward) as consume_count',//消耗业绩
				'sum(handwork_reward) as handwork_count',//手工提成
				'sum(epiboly_reward) as epiboly_count'//合作项目提成
			])
			->where($rewardWhereStr,$rewardBind)
			->asArray()->all();
			$list[$k]['sell_count'] = $employeeReward[0][0]['sell_count'];
			$list[$k]['consume_count'] = $employeeReward[0][0]['consume_count'];
			$list[$k]['handwork_count'] = $employeeReward[0][0]['handwork_count'];
			$list[$k]['epiboly_count'] = $employeeReward[0][0]['epiboly_count'];
			unset($employeeReward);
		}
		return $this->render('list',['page'=>$page,'list'=>$list,'filterDate'=>$filterDate]);
	}
	/**
	 * 添加员工
	 * @date: 2016-2-24 
	 * @author: futao
	 */
	public function actionAdd()
	{
		if(!$this->request->isPost){
			$this->layout = '/alert';
			$this->disableDebugtool();
			return $this->render('add');
		}
		$employeeName = $this->request->post('staff_name','');
		$employeeMobile = $this->request->post('staff_mobile','');
		$echo=function($msg,$state=0){
			echo "<script>";
			echo "parent.formResult('".$msg."','".$state."')";
			echo "</script>";
			exit;
		};
		if($employeeName==''){
			$echo ('姓名不能为空！');
		}
		if(!$this->checkMobile($employeeMobile) && $employeeMobile!='') $echo('手机号码错误!');
		$employeeModel = new BusEmployee();
		$employeeModel->employee_name = $employeeName;
		$employeeModel->employee_mobile = $employeeMobile;
		$employeeModel->shop_id = $this->userInfo->shop_id;
		$employeeModel->boss_uid = $this->userInfo->boss_uid;
		$employeeModel->login_name = $this->userInfo->login_name;
		if(!$employeeModel->save(false)){
			$this->error('添加失败！');
		}else{
			echo "<script>";
			echo "top.location.href='".Url::toRoute('employee/list')."'";
			echo "</script>";
		}

	}
	/**
	 * 修改员工信息
	 * @date: 2016-2-25 
	 * @author: futao
	 */
	public function actionUpdate()
	{
		$employeeName = $this->request->post('staff_name','');
		$employeeMobile = $this->request->post('staff_mobile','');
		$id = intval($this->request->post('id',0));
		if($employeeName==''){
			$this->error('姓名不能为空！');
		}
		if($id<1){
			$this->error('参数错误！');
		}
		if(!$this->checkMobile($employeeMobile) && $employeeMobile!='') $this->error('手机号码错误!');
		$where = $this->where;
		$where[] = 'employee_id = :id';
		$bind[':id'] = $id;
		$whereStr = implode(' and ', $where);
		$employeeModel = BusEmployee::find()->where($whereStr,$bind)->one();
		$employeeModel->employee_name = $employeeName;
		if($employeeMobile)$employeeModel->employee_mobile = $employeeMobile;
		if(!$employeeModel->save(false)){
			$this->error('修改失败！');
		}else{
			$this->redirect(Url::toRoute('employee/list'));
		}
	}	
	/**
	 * 员工离职
	 * @date: 2016-2-25 
	 * @author: futao
	 */
	public function actionChangestate()
	{
		$id = intval($this->request->get('id',0));
		if($id<1){
			$this->error('参数错误！');
		}
		$where = $this->where;
		$where[] = 'employee_id = :id';
		$bind[':id'] = $id;
		$whereStr = implode(' and ', $where);
		$employeeModel = BusEmployee::find()->where($whereStr,$bind)->one();
		$employeeModel->del_flag = Status::DELETE;
		if(!$employeeModel->save(false)){
			$this->error('修改失败！');
		}else{
			$this->redirect(Url::toRoute('employee/list'));
		}
	}
}